from mcp.server.fastmcp import FastMCP, Context
import subprocess
import shlex
from typing import Literal

mcp = FastMCP("OEC-Hardware MCP Server")

"""
面向软件：oec-hardware
所有 MCP tool 列表：

1. oech_server_service_control
   - 用途：控制 oech-server.service 服务的操作
   - 参数：action - 操作类型：start/stop/restart/reload
   - 返回结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

2. oech_service_control
   - 用途：控制 oech.service 服务的操作
   - 参数：action - 操作类型：start/stop/restart/reload
   - 返回结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

@mcp.tool()
def oech_server_service_control(action: Literal["start", "stop", "restart", "reload"]) -> dict:
    """
    控制 oech-server.service 服务的操作
    
    参数:
        action: 操作类型 - start(启动)、stop(停止)、restart(重启)、reload(重载)
    
    返回 JSON 结构:
        {
            "success": 是否成功执行,
            "command": 执行的命令,
            "exit_code": 退出码,
            "stdout": 标准输出,
            "stderr": 标准错误
        }
    """
    cmd = f"systemctl {action} oech-server.service"
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command execution timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def oech_service_control(action: Literal["start", "stop", "restart", "reload"]) -> dict:
    """
    控制 oech.service 服务的操作
    
    参数:
        action: 操作类型 - start(启动)、stop(停止)、restart(重启)、reload(重载)
    
    返回 JSON 结构:
        {
            "success": 是否成功执行,
            "command": 执行的命令,
            "exit_code": 退出码,
            "stdout": 标准输出,
            "stderr": 标准错误
        }
    """
    cmd = f"systemctl {action} oech.service"
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command execution timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()